package com.company.model;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.joda.time.DateTime;
import org.joda.time.Duration;

import java.text.SimpleDateFormat;

@Data // 自动生成getter, setter, toString, equals, hashCode
@Builder // 提供链式构建对象的能力
@NoArgsConstructor
@AllArgsConstructor
public class Task implements Comparable<Task> {
    private Integer id;
    private String title;
    private String content;
    private TaskType taskType;
    private int projectId;
    private Priority priority;
    private DateTime startTime;
    private DateTime endTime;
    private TaskStatus status;
    private DateTime createdTime;
    private DateTime updatedTime;
    private DateTime completedTime;

    // 使用Joda-Time进行日期比较和计算，更简洁
    public boolean isOverdue() {
        if (status == TaskStatus.已完成) return false;
        return new DateTime().isAfter(endTime);
    }

    public Duration getRemainingTime() {
        if (status == TaskStatus.已完成 || isOverdue()) return Duration.ZERO;
        return new Duration(new DateTime(), endTime);
    }

    // 用于TreeSet或List排序，按开始时间排序
    @Override
    public int compareTo(Task other) {
        return this.startTime.compareTo(other.startTime);
    }

    @Override
    public String toString() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return String.format("[%s] %-20s - %s (%s to %s)",
                priority, title, status,
                sdf.format(startTime.toDate()), sdf.format(endTime.toDate()));
    }

    public enum TaskType {日常事务,deadline,会议}
    public enum Priority {低,中,高}
    public enum TaskStatus {未开始,进行中,已完成,已取消,延迟}
}